using System;
using Oracle.ManagedDataAccess.Client;
using OracleInternal.Common;

namespace OracleInternal.TTC
{
	internal class TTCError : TTCMessage
	{
		internal const int OERFUPD = 8;

		internal const int OERFEXIT = 16;

		internal const int OERFNCF = 32;

		internal const int OERwNVIC = 4;

		internal const int OERwUDnW = 16;

		internal const int OERwCPER = 32;

		internal const int OERFPLSW = 4;

		internal TTCArrayBindError[] m_bindErrors;

		internal long m_curRowNumber;

		internal int m_retCode;

		private int m_arrayElemWError;

		private int m_arrayElemErrno;

		private int m_currCursorID;

		internal short m_errorPosition;

		private short m_sqlType;

		private byte m_oerFatal;

		internal short m_flags;

		private short m_userCursorOpt;

		private short m_upiParam;

		internal short m_warningFlag;

		private int m_osError;

		private short m_stmtNumber;

		private short m_callNumber;

		private int m_pad1;

		private long m_successIters;

		private int m_partitionId;

		private short m_tableId;

		private int m_slotNumber;

		private long m_rba;

		private long m_blockNumber;

		private int m_warnLength;

		internal int m_TTIWRNFlag;

		internal int m_ttiwrnNumber;

		internal byte[] m_ttiwrnMsg;

		private short[] m_errorLength = new short[1];

		internal byte[] m_errorMsg;

		internal int ErrorCode => m_retCode;

		internal byte[] ErrorMessage => m_errorMsg;

		internal int CursorId => m_currCursorID;

		internal short Flags => m_flags;

		internal TTCError(MarshallingEngine mEngine)
			: base(mEngine, 4)
		{
		}

		internal void Initialize()
		{
			m_bindErrors = null;
			m_retCode = 0;
			m_errorMsg = null;
			m_TTIWRNFlag = 0;
			m_warningFlag = 0;
			m_ttiwrnNumber = 0;
			m_ttiwrnMsg = null;
			m_errorPosition = -1;
		}

		internal int ReadErrorMessage()
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCError, OracleTraceFuncName.ReadErrorMessage);
			}
			try
			{
				if (m_marshallingEngine.HasEOCSCapability)
				{
					m_marshallingEngine.m_endOfCallStatus = m_marshallingEngine.UnmarshalUB4();
				}
				if (m_marshallingEngine.HasFSAPCapability)
				{
					m_marshallingEngine.m_endToEndECIDSequenceNumber = m_marshallingEngine.UnmarshalUB2();
				}
				m_curRowNumber = m_marshallingEngine.UnmarshalUB4();
				m_retCode = m_marshallingEngine.UnmarshalUB2();
				m_arrayElemWError = m_marshallingEngine.UnmarshalUB2(bIgnoreData: true);
				m_arrayElemErrno = m_marshallingEngine.UnmarshalUB2(bIgnoreData: true);
				m_currCursorID = m_marshallingEngine.UnmarshalUB2();
				m_errorPosition = (short)m_marshallingEngine.UnmarshalUB2();
				m_sqlType = m_marshallingEngine.UnmarshalUB1();
				m_oerFatal = (byte)m_marshallingEngine.UnmarshalUB1(bIgnoreData: true);
				m_flags = (short)m_marshallingEngine.UnmarshalUB2();
				m_userCursorOpt = (short)m_marshallingEngine.UnmarshalUB2(bIgnoreData: true);
				m_upiParam = m_marshallingEngine.UnmarshalUB1(bIgnoreData: true);
				m_warningFlag = m_marshallingEngine.UnmarshalUB1();
				m_rba = m_marshallingEngine.UnmarshalUB4(bIgnoreData: true);
				m_partitionId = m_marshallingEngine.UnmarshalUB2(bIgnoreData: true);
				m_tableId = m_marshallingEngine.UnmarshalUB1(bIgnoreData: true);
				m_blockNumber = m_marshallingEngine.UnmarshalUB4(bIgnoreData: true);
				m_slotNumber = m_marshallingEngine.UnmarshalUB2(bIgnoreData: true);
				m_osError = (int)m_marshallingEngine.UnmarshalUB4(bIgnoreData: true);
				m_stmtNumber = m_marshallingEngine.UnmarshalUB1(bIgnoreData: true);
				m_callNumber = m_marshallingEngine.UnmarshalUB1(bIgnoreData: true);
				m_pad1 = m_marshallingEngine.UnmarshalUB2(bIgnoreData: true);
				m_successIters = m_marshallingEngine.UnmarshalUB4(bIgnoreData: true);
				m_marshallingEngine.UnmarshalDALC(bIgnoreData: true);
				int num = m_marshallingEngine.UnmarshalUB2();
				if (num > 0)
				{
					m_bindErrors = new TTCArrayBindError[num];
					bool flag = m_marshallingEngine.UnmarshalUB1() == 254;
					for (int i = 0; i < num; i++)
					{
						if (flag)
						{
							if (m_marshallingEngine.m_bUseBigCLRChunks)
							{
								m_marshallingEngine.UnmarshalSB4();
							}
							else
							{
								m_marshallingEngine.UnmarshalUB1();
							}
						}
						m_bindErrors[i] = default(TTCArrayBindError);
						m_bindErrors[i].m_errorCode = m_marshallingEngine.UnmarshalUB2();
					}
					if (flag)
					{
						m_marshallingEngine.UnmarshalUB1();
					}
				}
				int num2 = (int)m_marshallingEngine.UnmarshalUB4();
				if (num2 > 0)
				{
					if (m_bindErrors == null)
					{
						m_bindErrors = new TTCArrayBindError[num2];
					}
					bool flag2 = m_marshallingEngine.UnmarshalUB1() == 254;
					for (int j = 0; j < num2; j++)
					{
						if (flag2)
						{
							if (m_marshallingEngine.m_bUseBigCLRChunks)
							{
								m_marshallingEngine.UnmarshalSB4();
							}
							else
							{
								m_marshallingEngine.UnmarshalUB1();
							}
						}
						m_bindErrors[j].m_rowOffset = (int)m_marshallingEngine.UnmarshalUB4();
					}
					if (flag2)
					{
						m_marshallingEngine.UnmarshalUB1();
					}
				}
				int num3 = m_marshallingEngine.UnmarshalUB2();
				if (num3 > 0)
				{
					if (m_bindErrors == null)
					{
						m_bindErrors = new TTCArrayBindError[num3];
					}
					m_marshallingEngine.UnmarshalUB1();
					int[] array = new int[1];
					for (int k = 0; k < num3; k++)
					{
						int buflen = m_marshallingEngine.UnmarshalUB2();
						m_bindErrors[k].m_errorMsg = m_marshallingEngine.UnmarshalCLR(buflen, array);
						m_bindErrors[k].m_errorLength = array[0];
						m_marshallingEngine.UnmarshalUB1();
						m_marshallingEngine.UnmarshalUB1();
					}
				}
				if (m_marshallingEngine.NegotiatedTTCVersion >= 7)
				{
					m_retCode = (int)m_marshallingEngine.UnmarshalUB4();
					m_curRowNumber = m_marshallingEngine.UnmarshalSB8();
				}
				if (m_sqlType == 3 && 1403 == m_retCode)
				{
					m_marshallingEngine.UnmarshalCLRforREFS(bIgnoreData: true);
				}
				else if (m_retCode != 0)
				{
					m_errorMsg = m_marshallingEngine.UnmarshalCLRforREFS();
					m_errorLength[0] = (short)m_errorMsg.Length;
				}
				return m_currCursorID;
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCError, OracleTraceFuncName.ReadErrorMessage, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCError, OracleTraceFuncName.ReadErrorMessage);
				}
			}
		}

		internal void ReadWarning()
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCError, OracleTraceFuncName.ReadWarning);
			}
			try
			{
				m_ttiwrnNumber = m_marshallingEngine.UnmarshalUB2();
				m_warnLength = m_marshallingEngine.UnmarshalUB2();
				m_TTIWRNFlag = m_marshallingEngine.UnmarshalUB2();
				if (m_ttiwrnNumber != 0 && m_warnLength > 0)
				{
					m_ttiwrnMsg = m_marshallingEngine.UnmarshalCHR(m_warnLength);
					m_errorLength[0] = (short)m_warnLength;
				}
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCError, OracleTraceFuncName.ReadWarning, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCError, OracleTraceFuncName.ReadWarning);
				}
			}
		}
	}
}
